/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.Windows;

namespace _videoConference.BAL
{
    public class Fileuploadcode
    {
        IsvcFileTransfer fileuploadproxy;
        System.IO.FileStream fs;
        int i = 0;
        byte[] arr = new byte[5000];
        byte[] Larr;

        public void servercred()
        {
            try
            {
                NetTcpBinding b = new NetTcpBinding();
                b.OpenTimeout = TimeSpan.MaxValue;
                b.PortSharingEnabled = true;

                ChannelFactory<IsvcFileTransfer> fileuploadfactory = new ChannelFactory<IsvcFileTransfer>(b);
                fileuploadfactory.Credentials.Windows.ClientCredential.UserName = UserInfo.CreUserName;
                fileuploadfactory.Credentials.Windows.ClientCredential.Password = UserInfo.CrePassword;
                fileuploadfactory.Credentials.Windows.ClientCredential.Domain = UserInfo.CreMachName;
                fileuploadfactory.Open();
                fileuploadproxy = fileuploadfactory.CreateChannel(new EndpointAddress("net.tcp://"+UserInfo.CreMachName+":2600/FileUpload"));                
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--Fileuploadcode.cs--:--servercred()--:--");
                clsUtility.LogException = exp;
            }
        }

        public void uploadfile(string filepath, string modulename)
        {
            try
            {
                if (!File.Exists(filepath))
                {
                    //MessageBox.Show("Specify correct file in text box to upload it!!", "File Upload");
                    return;
                }

                fileuploadproxy.svcSetFile(filepath);

                fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);

                if (fs.Length > 5000)
                {
                    for (i = 0; i < fs.Length / 5000; i++)
                    {
                        fs.Read(arr, 0, 5000);
                        fileuploadproxy.svcSendFile(arr);
                    }

                    if (i * 5000 != fs.Length)
                    {
                        Larr = new byte[int.Parse(fs.Length.ToString()) - (i * 5000)];
                        fs.Read(Larr, 0, int.Parse(fs.Length.ToString()) - (i * 5000));
                        fileuploadproxy.svcSendFile(Larr);
                    }
                }
                else
                {
                    Larr = new byte[int.Parse(fs.Length.ToString())];
                    fs.Read(Larr, 0, int.Parse(fs.Length.ToString()));
                    fileuploadproxy.svcSendFile(Larr);
                }
                fileuploadproxy.svcSaveFile();

                fs.Close();
                fs.Dispose();

                MessageBox.Show("Your zip file have been uploaded successfully!!", "File Upload");
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--Database.xaml.cs--:--uploadfile()--:--");
                clsUtility.LogException = exp;
            }
        }

        public void svcDeleteFolder(string foldername)
        {
            fileuploadproxy.svcDeleteFolder(foldername);
        }
    }
}
